<?php
/**
 *  后台文章管理的curd
 * Author: lyun
 * Date: 2018/1/26
 * Created by Panxsoft.
 */


namespace App\Http\Controllers\Admin;

use App\Http\Models\Article;
use App\Http\Models\ArticleTag;
use App\Http\Requests\Admin\ArticleIndexRequest;
use App\Http\Requests\Admin\ArticleStoreRequest;
use App\Http\Requests\Admin\ArticleUpdateRequest;
use App\Services\Utils\ApiChecker;
use App\Services\Utils\ApiResponser;
use App\Services\Utils\DeleteRichTextBosKey;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class ArticleController extends Controller
{
    use ApiChecker,ApiResponser;

    /**
     * Display a listing of the resource.
     *
     * @param ArticleIndexRequest $request
     * @return \Illuminate\Http\Response
     */
    public function index(ArticleIndexRequest $request)
    {
        $articles = Article::filterArticleTypeId($request->article_type_id)
            ->filterTitle($request->title)
            ->latest('sort')
            ->paginate($request->per_page?:15);

        return $this->response(compact('articles'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $article_types = ArticleTag::where('super_tag_id', ArticleTag::TOP_TAG)
            ->with('getSubTags')
            ->latest('sort')
            ->get();

        $types = ArticleTag::where('super_tag_id', '<>', ArticleTag::TOP_TAG)
            ->pluck('name', 'id')
            ->all();

        return $this->response(compact('article_types', 'types'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  ArticleStoreRequest  $request
     * @return \Illuminate\Http\Response
     */
    public function store(ArticleStoreRequest $request)
    {
        $article_data = $this->transformArticleData($request);

        // 检测是否是企业动态
        $article_data = $this->checkCarouselFigure($article_data);

        $stored = Article::create($article_data);

        return $this->response(compact('stored'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  ArticleUpdateRequest  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(ArticleUpdateRequest $request, $id)
    {
        /** @var Article $article */
        $article = Article::find($id);
            $this->checkItemExist($article, '文章');

        $article_data = $this->transformArticleData($request);

        // 检测是否是企业动态
        $article_data = $this->checkCarouselFigure($article_data);

        // 更新富文本中的图片，删除原来的boskey
        $new_content = $article_data['content'];
        $old_content = $article->content;
        (new DeleteRichTextBosKey)->updateRichTextBosKey($new_content, $old_content);

        $updated = $article->update($article_data);

        return $this->response(compact('updated'));
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        /** @var Article $article */
        $article = Article::find($id);
            $this->checkItemExist($article, '文章');

        // 删除富文本的boskey的值
        (new DeleteRichTextBosKey)->deleteRichTextBosKey($article->content);

        $deleted = $article->delete();

        return $this->response(compact('deleted'));
    }

    /**
     * @param ArticleStoreRequest|ArticleUpdateRequest|Request $request
     * @return array
     */
    private function transformArticleData(Request $request)
    {
        return array(
            'article_type_id'     => $request->article_type_id,
            'title'               => $request->title,
            'thumb'               => $request->thumb,
//            'banner'              => $request->banner,
            'carousel_figure'     => $request->carousel_figure,
//            'company_thumb'       => $request->company_thumb,
            'description'         => $request->description,
            'english_description' => $request->english_description,
            'english_content'     => $request->english_content,
            'english_title'       => $request->english_title,
            'sort'                => $request->sort?:0,
            'content'             => $request->content,
            'keywords'            => $request->keywords,
        );
    }

    /**
     * @param array $article_data
     * @return array
     */
    private function checkCarouselFigure(array $article_data)
    {
        if (Article::checkIsCompanyNews($article_data['article_type_id'])) {
            $this->checkCustomValue($article_data['carousel_figure'], '企业动态下的文章必须上传轮播图');
        } else {
            unset($article_data['carousel_figure']);
        }

        return $article_data;
    }
}
